home *** CD-ROM | disk | FTP | other *** search
- Path: lantana.singnet.com.sg!usenet
- From: s8700055@singnet.com.sg (XY Xie)
- Newsgroups: comp.lang.c
- Subject: Question on 'bubble sort'
- Date: Sat, 30 Mar 1996 05:59:44 GMT
- Organization: Singapore Telecom Internet Service
- Message-ID: <4jieso$juc@lantana.singnet.com.sg>
- NNTP-Posting-Host: ts900-2816.singnet.com.sg
- X-Newsreader: Forte Free Agent 1.0.82
-
- I came across this sorting algorithm called 'bubble sort' in a book.
- Here is a part of the book's code for sorting an array of integers in
- ascending order :
-
- for(outer=0 ; outer<(NUM-1) ; outer++)
- {
- //variable definitions here......
- int didswap = 0; //didswap remains zero if no swap occurs below
- //my question is on 'didswap'
-
-
- for (inner=outer ; inner< NUM ; inner++)
- {
- if ( arrayname[outer] > arrayname[inner])
- {
- //code that swaps values follows
- didswap = 1 //didswap = 1 if a swap took place
- }
- }
-
- if ( ! didswap) //*returns 1 if swap took place*(relates to question)
-
- { break ; } //outer for loop terminated
-
- }
-
- My QUESTION : if the smallest integer happens to be the first member
- initially , would not the 'if ( !didswap ) ' test terminate the
- sorting procedure prematurely , with the rest of the list unsorted ?
- If not , why ?
- Someone pls enlighten me . Pls email me direct . THANKs for the
- trouble .
-
- XY Xie
- s8700055@singnet.com.sg
-
-